package demo1;

import java.util.HashMap;
import java.util.List;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //矩阵区域和
    public int[][] matrixBlockSum(int[][] mat, int k) {
        int m =mat.length;int n = mat[0].length;
        int[][] dp = new int[m+1][n+1];
        int[][] ret = new int[m][n];
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                dp[i][j] = dp[i][j-1]+dp[i-1][j]+mat[i-1][j-1]-dp[i-1][j-1];
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int x1 = Math.max(0,i-k)+1;
                int y1 = Math.max(0,j-k)+1;
                int x2 = Math.min(m-1,i+k)+1;
                int y2 = Math.min(n-1,j+k)+1;
                ret[i][j] = dp[x2][y2]-dp[x2][y1-1]-dp[x1-1][y2]+dp[x1-1][y1-1];
            }
        }
        return ret;
    }


    //连续数组
    public int findMaxLength(int[] nums) {
        HashMap<Integer,Integer> map = new HashMap<>();
        int len = 0;
        int sum = 0;
        map.put(0,-1);
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]==1) sum+=1;
            else sum-=1;
            if(map.containsKey(sum)) {
                len = Math.max(len, i - map.getOrDefault(sum, Integer.MAX_VALUE));
            }
            else{
                map.put(sum,i);
            }
        }
        return len;
    }

    public static void main1(String[] args) {
        System.out.println(-7%5);
    }

    //和可被k整除的子数组
    public int subarraysDivByK(int[] nums, int k) {
        HashMap<Integer,Integer> map = new HashMap<>();
        map.put(0,1);
        int count = 0;
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            count += map.getOrDefault((sum%k+k)%k,0);
            map.put((sum%k+k)%k,map.getOrDefault((sum%k+k)%k,0)+1);
        }
        return count;
    }


    //大礼包
    public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
        return 0;
    }
}
